package entities;

//version 1.0

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import entities.AcdcProgram;

public class Course {

	private String code;
	// This is the array of all section related to this course
	private static HashMap<String, Course> courseHm = new HashMap<String, Course>();
	// all courses
	private ArrayList<String> lectureArr = new ArrayList<String>();
	private ArrayList<String> tutorialArr = new ArrayList<String>();
	private String[] program;

	public Course(String code, String[] prgm) {
		this.code = code;
		if (courseHm.get(code) == null)
			courseHm.put(code, this);
		int size = 0;
		if (prgm != null) {
			size = prgm.length;
		}
		this.program = new String[size];
		for (int i = 0; i < size; i++) {
			this.program[i] = prgm[i];
		}
		for (int i = 0; i < this.program.length; i++) {
			AcdcProgram programTmp = AcdcProgram.getProgramByCode(prgm[i]);
			programTmp.addCourse(code);
		}
	}

	public void addLecture(String sectionId) {
		this.lectureArr.add(sectionId);
	}

	public void addTutorial(String sectionId) {
		this.tutorialArr.add(sectionId);
	}

	public String[] getAllLecture() {
		int arrSize = lectureArr.size();
		String[] sectionId = new String[arrSize];
		for (int i = 0; i < arrSize; i++)
			sectionId[i] = lectureArr.get(i);
		return sectionId;
	}

	public String[] getAllTutorial() {
		int arrSize = tutorialArr.size();
		String[] sectionId = new String[arrSize];
		for (int i = 0; i < arrSize; i++)
			sectionId[i] = tutorialArr.get(i);
		return sectionId;
	}

	public static String[] getAllCourses() {
		Iterator<String> sectionId = courseHm.keySet().iterator();
		String[] courseCodeList = new String[courseHm.size()];
		int i = 0;
		while (sectionId.hasNext()) {
			courseCodeList[i++] = sectionId.next();
		}
		Arrays.sort(courseCodeList);

		return courseCodeList;
	}

	// get the particular course by course code
	public static Course getCourseByCode(String code) {
		return courseHm.get(code);
	}

	public String getCourseCode() {
		return this.code;
	}

	public String[] getPrograms() {
		return this.program;
	}

	public static void clear() {
		courseHm.clear();
	}
}
